package cn.com.api.service.impl;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiShop;
import cn.com.api.entity.ApiT_Cash;
import cn.com.api.entity.ApiT_shops_apply;
import cn.com.api.entity.ApiT_shops_pay_records;
import cn.com.api.mapper.ApiShopsMapper;
import cn.com.api.mapper.ApiT_shop_pay_recordsMapper;
import cn.com.api.mapper.ApiT_shops_applyMapper;
import cn.com.api.mapper.ApiT_statsMapper;

import cn.com.api.service.ApiT_shops_applyService;
import cn.com.api.service.impl.base.T_shops_applyServiceImplBase;
import cn.com.util.DateUtil;
import cn.com.util.*;

/**
 * 
 **/
@Service
public class ApiT_shops_applyServiceImpl extends T_shops_applyServiceImplBase implements ApiT_shops_applyService{
	@Autowired
	private ApiT_shops_applyMapper apiT_shops_applyMapper;
	@Autowired
	private ApiShopsMapper apiShopsMapper;
	@Autowired
	private ApiT_statsMapper statsMapper;
	@Autowired
	private ApiT_shop_pay_recordsMapper apiT_shop_pay_recordsMapper;

	@Override
	public void addApply(ApiT_shops_apply apply) {
		DecimalFormat df = new DecimalFormat("######0.00");
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//ApiShop shop=apiShopsMapper.findT_shopsById(Integer.parseInt(apply.getShop_id()));
		List<Map<String,Object>> stats=new ArrayList<Map<String,Object>>();
		stats=statsMapper.shopStatAll(apply.getShop_id());
		int orderCount=0;
		Double orderPrice=0.0;
		for(Map<String,Object> m:stats){
			orderCount+=Integer.parseInt(m.get("order_count")+"");
			orderPrice=Double.parseDouble(df.format(orderPrice+Double.parseDouble(m.get("orders_money")+"")));
		}
		Double avg=0.0;
		if(orderCount>0){
			avg=Double.parseDouble(df.format(orderPrice/orderCount));
		}		apply.setDay_avg_money(avg);
		apply.setStatus(0);
		apply.setCreate_time(format.format(new Date()));
		apiT_shops_applyMapper.insertT_shops_apply(apply);
	}

	@Override
	public List<Map<String, Object>> searAll(String sname,String stime,String etime) {
		return apiT_shops_applyMapper.searAll(sname,stime,etime);
	}

	@Override
	public boolean updateMoneyAndRem(String money, String remark, String id) {
		return apiT_shops_applyMapper.updateMoneyAndRem(money, remark, id);
	}

	@Override
	public Map<String, Object> searById(String id) {
		return apiT_shops_applyMapper.searById(id);
	}

	@Override
	public boolean updateCheck(String lose, String status, String id) {
		return apiT_shops_applyMapper.updateCheck(lose, status, id);
	}

	@Override
	public boolean updatePayMoney(String num, String id) {
		return apiT_shops_applyMapper.updatePayMoney(num, id);
	}

	@Override
	public boolean updateMoneyAndRemAndStatus(String money, String remark,
			String status, String id) {
		return apiT_shops_applyMapper.updateMoneyAndRemAndStatus(money, remark, status, id);
	}


	@Override
	public List<Map<String, Object>> searByShopLog(String shopid) {
		//return apiT_shops_applyMapper.searByShopLog(shopid);
		return null;
	}
	@Override
	public List<ApiT_shops_apply> checkedTransferList(ApiT_shops_apply conditions, PageInfo pageInfo) {
		conditions.setStatus(1);//
		return apiT_shops_applyMapper.transferListPage(conditions, pageInfo);
	}

	@Override
	public List<ApiT_Cash> checkedCashTransferList(ApiT_Cash conditions, PageInfo pageInfo) {
		conditions.setStatus(1);//
		return apiT_shops_applyMapper.cashListPage(conditions, pageInfo);
	}

	@Override
	@Transactional
	public int updatCashPay(int cashId,int shopId,String applyMondy,ApiT_shops_pay_records payRecord) {
		ApiShop shop = apiShopsMapper.findT_shopsById(shopId);
		Double cashMoney = shop.getCashMoney();//商户余额
		
		ApiT_Cash newCash = new ApiT_Cash();
		String handle_time = DateUtil.nowTimeToString();
		newCash.setHandle_time(handle_time);
		newCash.setUpdatePayed(true);
		newCash.setId(cashId);
		newCash.setAfter_money(cashMoney);
		int count =  apiT_shops_applyMapper.updateCash(newCash);
		
		//添加打款记录
//		payRecord.setAfter_money(cashMoney);
		payRecord.setTime(handle_time);
		payRecord.setStats_id(cashId);
		payRecord.setType(3);//3:商家提现申请打款
		payRecord.setShop_id(shopId);
		payRecord.setNum(new Date().getTime() + shopId+"");
		apiT_shop_pay_recordsMapper.insertT_shop_pay_record(payRecord);
		
		return count;
	}

}


